package com.hm.shopping.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.hm.common.su.bean.PageInfo;
import com.hm.common.su.bean.PageInfo.PageInfoParam;
import com.hm.common.su.bean.ServerResponse;
import com.hm.shopping.auto.su.ControllerResult;
import com.hm.shopping.auto.su.R.Restful;
import com.hm.shopping.auto.su.helper.HmSessionFactory;
import com.hm.shopping.def.NewsAnnouncementEnum;
import com.hm.shopping.service.NewsAnnouncementService;
import com.hm.shopping.vo.NewsAnnouncementVo;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;

/**
 * @author shishun.wang
 * @date 2018年1月31日 下午5:09:10
 * @version 1.0
 * @describe 新闻公告
 */
@Slf4j
@ResponseBody
@RestController
@RequestMapping(Restful.API + "/newsann")
public class NewsAnnouncementAction {
	
	@Autowired
	private NewsAnnouncementService newsAnnouncementService;

	private static final String SWWAGER_TAG = "新闻公告管理";
	
	@RequestMapping(method = RequestMethod.POST)
	@ApiOperation(tags = SWWAGER_TAG, value = "新增新闻公告信息", httpMethod = "POST", notes = "com.hm.shopping.api.NewsAnnouncementAction::addNewsAnnouncement")
	public ResponseEntity<ServerResponse<Boolean>> addNewsAnnouncement(
			@ApiParam(required = true, name = "body") @RequestBody(required = true) NewsAnnouncementVo newsAnnouncementVo) {
		log.debug("用户{}新增新闻公告信息", HmSessionFactory.currentUserId());
		newsAnnouncementVo.setCreateUser(HmSessionFactory.currentUserId());
		newsAnnouncementService.addNewsAnnouncement(newsAnnouncementVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(value = "/{newsAnnouncementId}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "获取新闻公告信息", httpMethod = "GET", notes = "com.hm.shopping.api.NewsAnnouncementAction::getNewsAnnouncement")
	public ResponseEntity<ServerResponse<NewsAnnouncementVo>> getNewsAnnouncement(
			@ApiParam(required = true, name = "newsAnnouncementId", value = "新闻公告ID") @PathVariable("newsAnnouncementId") String newsAnnouncementId) {
		log.debug("用户{}获取新闻公告信息", HmSessionFactory.currentUserId());
		return ControllerResult.success(newsAnnouncementService.getNewsAnnouncement(newsAnnouncementId));
	}

	@RequestMapping(value = "/{newsAnnouncementId}", method = RequestMethod.PUT)
	@ApiOperation(tags = SWWAGER_TAG, value = "修改新闻公告信息", httpMethod = "PUT", notes = "com.hm.shopping.api.NewsAnnouncementAction::updateNewsAnnouncement")
	public ResponseEntity<ServerResponse<Boolean>> updateNewsAnnouncement(
			@ApiParam(required = true, name = "newsAnnouncementId", value = "新闻公告ID") @PathVariable("newsAnnouncementId") String newsAnnouncementId,
			@ApiParam(required = true, name = "body") @RequestBody(required = true) NewsAnnouncementVo newsAnnouncementVo) {
		log.debug("用户{}修改新闻公告信息", HmSessionFactory.currentUserId());
		newsAnnouncementVo.setId(newsAnnouncementId);
		newsAnnouncementService.updateNewsAnnouncement(newsAnnouncementVo);
		return ControllerResult.success(true);
	}

	@RequestMapping(method = RequestMethod.DELETE)
	@ApiOperation(tags = SWWAGER_TAG, value = "删除新闻公告信息", httpMethod = "DELETE", notes = "com.hm.shopping.api.NewsAnnouncementAction::deleteNewsAnnouncement")
	public ResponseEntity<ServerResponse<Boolean>> deleteNewsAnnouncement(
			@ApiParam(required = true, name = "newsAnnouncementId", value = "新闻公告ID") @RequestParam(required = true, name = "newsAnnouncementId") String newsAnnouncementId) {
		log.debug("用户{}删除新闻公告信息", HmSessionFactory.currentUserId());
		newsAnnouncementService.deleteNewsAnnouncement(newsAnnouncementId);
		return ControllerResult.success(true);
	}
	
	@RequestMapping(value = "/{pageNumber}/{pageSize}", method = RequestMethod.GET)
	@ApiOperation(tags = SWWAGER_TAG, value = "分页新闻公告记录", httpMethod = "GET", notes = "com.hm.shopping.api.NewsAnnouncementAction::queryNewsAnnouncement")
	public ResponseEntity<ServerResponse<PageInfo<NewsAnnouncementVo>>> queryNewsAnnouncement(
			@ApiParam(required = true, name = "pageNumber", value = "分页号(第一页值为1,最小页码不能小于1)") @PathVariable("pageNumber") int pageNumber,
			@ApiParam(required = true, name = "pageSize", value = "分页大小") @PathVariable("pageSize") int pageSize,
			@ApiParam(required = false, name = "newsAnnouncement", value = "新闻公告类型") @RequestParam(required = false, name = "newsAnnouncement") NewsAnnouncementEnum newsAnnouncement,
			@ApiParam(required = false, name = "newsName", value = "新闻标题") @RequestParam(required = false, name = "newsName") String newsName){
		log.debug("用户{},查询分页新闻公告记录", HmSessionFactory.currentUserId());
		return ControllerResult.success(newsAnnouncementService.queryNewsAnnouncement(new PageInfoParam(pageNumber, pageSize), newsAnnouncement, newsName));
	}
}
